package com.bwie.record.consumer;

import com.alibaba.fastjson.JSON;
import com.bwie.record.domain.DataOne;
import com.bwie.record.service.DataOneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class DataConsumer1 {
    @Autowired
    private DataOneService dataOneService;

    @RabbitListener(queues = "dataOneQueue")
    public void consumeDataOne(String dataOne) {
        DataOne data = JSON.parseObject(dataOne, DataOne.class);
        log.info("消费者1接收到数据： " + data +", 时间："+ new Date().toLocaleString());
        //消费到数据后，存储到mysql的表（自建）中
        //根据消费到的id，去数据库查询是否存在，存在则修改、不存在则插入
        DataOne dataOneInfo = dataOneService.getDataOneById(data.getId());
        if (dataOneInfo == null) {
            dataOneService.addDataOne(data);
        } else {
            dataOneService.updateDataOne(data);
        }
    }
}
